import os

from appium import webdriver
from time import sleep
from appium.webdriver.common.touch_action import TouchAction
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

caps = {
    'platformName': 'Android',  # 什么系统
    'platformVersion': '7.1.2',  # 版本号
    'deviceName': '8GP7N18523000526',  # 名字 安卓随便
    # 'appPackage': 'io.cordova.hellocordovauat',  # Sit包名
    'appPackage': 'com.kwl.bhtapp',  # 包名
    'appActivity': '.activity.StartActivity',  # 界面名
    'automationName': 'Uiautomator2',
    'autoWebviewTimeout': 8000,  # 等待时间
    'unicodeKeyboard': True,  # 使用自带输入法输入中文时填 true
    'resetKeyboard': True,  # 执行完程序恢复原来输入法
    # 'noReset': True,  # 不要重置app
}


def findall(driver, el):
    source = driver.page_source
    # print(source)
    try:
        if el in source:
            return True
        else:
            return False
    except NoSuchElementException:
        return False


result = []
# 获取driver
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_capabilities=caps)
driver.implicitly_wait(10)
# 首次安装同意协议
driver.find_element(By.ID, "com.kwl.bhtapp:id/btn_agree").click()
sleep(2)
driver.swipe(900, 1000, 100, 1000)
print(driver.contexts)
driver.find_element_by_xpath("//android.widget.TextView[@text='立即体验']").click()

for i in driver.contexts:
    if i.lower().startswith("webview"):
        driver.switch_to.context(i)
        break
driver.wait_activity(".MainActivity", 3)
sleep(2)
# print(driver.contexts)
# sleep(3)
driver.find_element_by_id("username").clear()
sleep(2)
aa = [1, 5, 3, 1, 7, 9, 5, 8, 9, 8, 1]
# aa = [1, 7, 7, 0, 3, 2, 2, 9, 9, 0, 1]
for i in aa:
    driver.find_element_by_id("username").send_keys(i)
sleep(1)
driver.find_element_by_id("password").send_keys("wangdi822451")
sleep(2)
driver.find_element_by_css_selector('[type = "checkbox"]').click()  # 协议
sleep(2)
driver.find_element_by_css_selector('.am-button.am-button-primary').click()  # 登录按钮
sleep(5)
try:  # 查看是否有消息弹出框
    # if_Notice = driver.find_element(By.CSS_SELECTOR, ".am-modal-content")
    # 有弹窗时
    driver.find_element(By.CSS_SELECTOR, ".am-modal-button").click()
except Exception as e:
    pass  # 没有弹窗时
if findall(driver, "综合视频"):
    # 点击综合视频
    driver.find_element_by_xpath('.//*[@id="app-root"]/div/div/div[4]/div[2]/div[2]').click()
    # 当前上下文位置
    print(driver.current_context)
    print("全部的上下文：%s" % driver.contexts)
    # 切换到原生NATIVE_APP
    driver.switch_to.context(driver.contexts[0])
    print(driver.current_context)
    # 允许
    for i in range(0, 3):
        driver.find_element(By.ID, "com.android.packageinstaller:id/permission_allow_button").click()
        sleep(1)
    for i in driver.contexts:
        if i.lower().startswith("webview"):
            driver.switch_to.context(i)
            break
    driver.find_element_by_xpath('.//*[@id="app-root"]/div/div/div[4]/div[2]/div[2]').click()
    sleep(5)
    print(driver.window_handles)
    driver.switch_to.window(driver.window_handles[-1])
    sleep(1)
    for i in driver.contexts:
        if i.lower().startswith("native"):
            driver.switch_to.context(i)
            break
    # content = driver.find_element_by_id("com.kwl.bhtapp:id/ytx_action_title").text
    # 点击“PICC直播课堂”
    print("点击picc")
    driver.find_element_by_xpath("//*[@content-desc='PICC直播课堂']/android.widget.TextView[1]").click()
    # 定位搜索框
    print("搜索框")
    ele = driver.find_element_by_id("com.kwl.bhtapp:id/search_box_start_search")
    ele.click()
    # 切换键盘搜狗
    os.system("adb shell ime set com.sohu.inputmethod.sogou/.SogouIME")
    ele = driver.find_element_by_id("com.kwl.bhtapp:id/search_expanded_edit_text")  # 定位搜索框
    ele.send_keys("如何使用调查问卷")

    ele.click()
    driver.tap([(980, 1860)], 2)  # 点击搜狗输入法“搜索”按钮
    driver.find_element_by_id("com.kwl.bhtapp:id/tv_title").click()  # 点击“如何使用调查问卷”标题进入
    sleep(4)
    content = driver.find_element_by_id("com.kwl.bhtapp:id/ytx_top_title").text  # 获取视频标题

    if content == "如何使用调查问卷":  # 打印综合视频
        print("综合视频加载成功")
        # result.append("\n综合视频加载成功 ")
        driver.find_element_by_id("com.kwl.bhtapp:id/ytx_close_live").click()  # 点击退出视频播放
        sleep(1)
        driver.back()
    else:
        ifError = True
        print("综合视频******************进入失败！")
        # result.append("\n综合视频进入失败1*************")

    sleep(1)
    driver.back()
else:
    ifError = True
    # print(driver.page_source)
    print("综合视频******************进入失败！")
    # result.append("\n综合视频进入失败2*************")

driver.switch_to.context(driver.contexts[-1])
sleep(1)
# print("进入钱包")
# result.append("\n进入钱包 ")
# driver.find_elements_by_css_selector(".am-tab-bar-tab-title")[-2].click()  # 钱包

# sleep(3)
# driver.find_element_by_xpath(".//*[@id='app-root']/div/div[2]/div/div[2]/div[2]/div/div/div[2]").click()  # 点击“人保钱包”
# print("进入人保钱包选择")
# sleep(3)
#
# 选择兴业钱包开始************
# driver.find_element_by_id("xingye").click()
# print("选择兴业")
# result.append("\n选择兴业钱包 ")
# sleep(6)
# print(driver.page_source)
# driver.find_element_by_css_selector(".src-pages-wallet-index-indexdisplayban___L5OO_").click()
# print("点开眼睛")
# result.append("\n点开眼睛 ")
# driver.find_element_by_id("eyes2").click()
# sleep(2)
# print("显示兴业钱包余额：")
# print(driver.find_element_by_id("money1").text)
# result.append("\n显示兴业钱数 ")
# sleep(3)
# 选择兴业钱包结束************
# driver.back()
# driver.find_element_by_xpath(".//*[@id=‘app-root’]/div/div[1]/div[1]").click()
sleep(3)
# 选择光大钱包开始************
# -----------------------------------------------
# driver.find_element_by_xpath(
#     ".//*[@id='app-root']/div/div[2]/div/div[2]/div[2]/div/div/div[2]").click()  # 点击“人保钱包”
# sleep(1)
# print("进入人保钱包选择")
# driver.find_element_by_id("guangda").click()
# print("选择光大")
# # result.append("\n选择光大钱包 ")
# sleep(10)
# print("点开眼睛")
# # result.append("\n点开眼睛 ")
# sleep(1)
# driver.find_element_by_id("eyes2").click()
# print("显示光大钱包余额：")
# sleep(1)
# print(driver.find_element_by_id("money1").text)
# # result.append("\n显示光大钱数 ")
# # print(driver.find_element_by_css_selector(".src-pages-wallet-index-indexhiddenBanSpan___Ik0kF").text)
# # 选择光大钱包结束************
# driver.back()
sleep(3)
# print("进入我的")
# result.append("\n进入我的 ")
# driver.find_element_by_xpath(".//*[@id='app-root']/div/div[2]/div/div[2]/div[7]").click()
# print("进入账户安全险")
# sleep(1)
# print(driver.find_element_by_xpath(".//*[@id='app-root']/div[4]/div/div[2]/div[1]/div[2]/div[1]/div[2]").text)
# driver.back()
sleep(3)

driver.find_elements_by_css_selector(".am-tab-bar-tab-title")[-1].click()  # 进入“我的”
sleep(2)
# driver.find_element_by_xpath("//*[@id='app-root']/div/div[2]/div/div[2]/div[4]/div/div").click()  #点击"员工认证“
# sleep(3)


# driver.find_element_by_xpath(".//*[@id='app-root']/div/div[2]/div/div[2]/div[7]").click()
driver.find_element_by_xpath(".//*[@id='app-root']/div/div[2]/div/div[2]/div[8]/div/div").click()  # 点击“设置”
print("点击设置")
# result.append("\n点击设置 ")
sleep(1)
driver.find_element_by_css_selector(".am-button").click()
print("退出登录，完美！")
sleep(1)
driver.find_element_by_link_text("确定").click()
# result.append("\n退出登录")

sleep(2)
driver.quit()

# sleep(8)
# driver.find_element_by_xpath('//*[@id="app-root"]/div/div/div[6]/div[1]/div/div[2]/div/div[1]').click()  # shouye
# driver.find_element_by_xpath('//*[text() = "员工认证"]').click()
# sleep(10)
# driver.find_element_by_xpath('//*[text() = "所属公司"]').click()
# sleep(2)
#
# while True:
#
#     driver.switch_to.context(driver.contexts[0])
#     driver.swipe(800, 1300, 800, 1200, 1600)
#     driver.switch_to.context(driver.contexts[1])
#     get = driver.find_element_by_css_selector('div.am-picker-col-item.am-picker-col-item-selected ').text  # 获取文本
#     print(get)
#     if "保互通(北京)有限公司" == get:
#         break
# driver.find_element_by_xpath('//*[text() = "确定"]').click()
#
# driver.find_element_by_xpath('//*[text() = "员工类型"]').click()
# while True:
#
#     driver.switch_to.context(driver.contexts[0])
#     driver.swipe(800, 1300, 800, 1200, 1600)
#     driver.switch_to.context(driver.contexts[1])
#     get = driver.find_element_by_css_selector('div.am-picker-col-item.am-picker-col-item-selected ').text
#     print(get)
#     if "外勤" == get:
#         break
# driver.find_element_by_xpath('//*[text() = "确定"]').click()
# sleep(3)
# driver.find_element_by_xpath('//input[@placeholder= "请输入员工工号" ]').send_keys("123456")
# it = driver.find_element_by_xpath('//input[@placeholder= "请输入员工姓名" ]')
# it.clear()
# it.send_keys("王迪")
# sleep(2)
# caid = driver.find_element_by_xpath('//input[@placeholder= "请输入员工身份证号" ]')
# caid.clear()
# caid.send_keys("610527199611104530")
# driver.find_element_by_xpath('//*[text() = "确 定"]').click()
# sleep(15)
# driver.find_element_by_xpath('//*[text() = "确定"]').click()  # 等待不确定
# sleep(8)
#
# driver.find_element_by_xpath('//*[@id="app-root"]/div/div/div[6]/div[1]/div/div[2]/div/div[5]/p').click()  # 我的
# sleep(2)
# driver.find_element_by_css_selector('svg.am-icon.am-icon-down.am-icon-md').click()
# if driver.find_element_by_xpath('//*[text() = "已认证"] ').is_displayed():
#     print("认证成功")
# else:
#     print("未认证")
#
# # driver.find_element_by_css_selector('[type = "checkbox"]').click()
# # sleep(2)
# # driver.find_element_by_css_selector('.am-button.am-button-primary').click()
# # sleep(3)
# # driver.find_elements_by_xpath("//*[text()= '商城']")[1].click()
# # driver.wait_activity(".activity.MyWebviewActivity", 5)
# # sleep(5)
# # print(driver.window_handles)
# # handles = driver.window_handles
# # for i in handles:
# #     driver.switch_to.window(i)
# #     # print(driver.page_source)
# #     print(driver.current_window_handle)
# #     if "保互通商城" in driver.page_source:
# #         break
